package com.iwhalecloud.bss.kite.cucc.web.controller.reception;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.cust.ILatestCustomerService;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.LatestEntertainCustomerDTO;
import com.iwhalecloud.bss.kite.cucc.client.vo.cust.LatestEntertainCustomerVO;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.ztesoft.bss.common.util.ContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
* @Description: TODO
*
* @author lyx
* @date 2020年11月18日
* @version V1.0
* @Package com.iwhalecloud.bss.kite.cucc.web.controller.reception
*/
@RestController
@RequestMapping("/latestCustomer")
@Api(tags = {"/latestCustomer - 最近受理10位客户"})
public class LatestCustomerController {

    @Autowired
    ILatestCustomerService latestCustomerService;

    @ApiOperation(value = "查询最近受理的10位客户", notes = "根据工号查询最近受理的10位客户")
    @ApiImplicitParams({@ApiImplicitParam(name = "staffId", value = "员工Id", required = false)})
    @GetMapping("listLatestCustomer")
    public DataResult<List<LatestEntertainCustomerVO>> listLatestCustomer(@RequestParam(required = false) String staffId) {
        if (KiteStringUtils.isEmpty(staffId)) {
            staffId = KiteStringUtils.valueOf(ContextUtil.getUserId());
        }
        List<LatestEntertainCustomerDTO> latestList = latestCustomerService.listLatestCustomer(staffId, ContextUtil.getLandId());
        List<LatestEntertainCustomerVO> latestVOList = new ArrayList<>();
        latestList.forEach(latest -> {
            LatestEntertainCustomerVO vo = KiteBeanUtils.transform(latest, LatestEntertainCustomerVO.class);
            vo.setCustId(KiteStringUtils.valueOf(latest.getCustId()));
            vo.setCbssId(latest.getCbssCustCode());
            vo.setGroupId(latest.getGroupCustCode());
            vo.setOrgCode(latest.getCreditCode());
            vo.setCertiCode(latest.getCertiNum());
            vo.setCertiType(latest.getCertiType());
            vo.setProvinceCode(latest.getProvinceId());
            vo.setProvinceName(LocalAttrHelper.getRegionName(latest.getProvinceId()));
            vo.setEparchyCode(latest.getLanId());
            vo.setEparchyName(LocalAttrHelper.getRegionName(latest.getProvinceId()) + "-"
                + LocalAttrHelper.getRegionName(latest.getLanId()));
            vo.setCustState(latest.getCustState());
            latestVOList.add(vo);
        });
        return ErrorConsts.SUCCESS.getResult(latestVOList);
    }


    @ApiOperation(value = "更新最近受理的10位客户", notes = "更新最近受理的10位客户")
    @ApiImplicitParams({@ApiImplicitParam(name = "key", value = "缓存key", required = false)})
    @PostMapping("dealLatestCustomer")
    public DataResult<Boolean> dealLatestCustomer(String staffId, String orgId, CuccContextCustDTO custDto) {
        String lanId = ContextUtil.getLandId();
        latestCustomerService.dealLatestCustomer(staffId, orgId, lanId, custDto);
        return ErrorConsts.SUCCESS.getResult(Boolean.TRUE);
    }

    @ApiOperation(value = "查询最近受理的10位云账户客户", notes = "根据工号查询最近受理的10位云账户客户")
    @ApiImplicitParams({@ApiImplicitParam(name = "staffId", value = "员工Id", required = false)})
    @GetMapping("listLatestCustomerCloud")
    public DataResult<List<LatestEntertainCustomerVO>> listLatestCustomerCloud(@RequestParam(required = false) String staffId) {
        if (KiteStringUtils.isEmpty(staffId)) {
            staffId = KiteStringUtils.valueOf(ContextUtil.getUserId());
        }
        List<LatestEntertainCustomerDTO> latestList = latestCustomerService.listLatestCustomerCloud(staffId, ContextUtil.getLandId());
        List<LatestEntertainCustomerVO> latestVOList = new ArrayList<>();
        latestList.forEach(latest -> {
            LatestEntertainCustomerVO vo = KiteBeanUtils.transform(latest, LatestEntertainCustomerVO.class);
            vo.setCustId(KiteStringUtils.valueOf(latest.getCustId()));
            vo.setCbssId(latest.getCbssCustCode());
            vo.setGroupId(latest.getGroupCustCode());
            vo.setOrgCode(latest.getCreditCode());
            vo.setCertiCode(latest.getCertiNum());
            vo.setCertiType(latest.getCertiType());
            vo.setProvinceCode(latest.getProvinceId());
            vo.setProvinceName(LocalAttrHelper.getRegionName(latest.getProvinceId()));
            vo.setEparchyCode(latest.getLanId());
            vo.setEparchyName(LocalAttrHelper.getRegionName(latest.getProvinceId()) + "-"
                + LocalAttrHelper.getRegionName(latest.getLanId()));
            vo.setCustState(latest.getCustState());
            latestVOList.add(vo);
        });
        return ErrorConsts.SUCCESS.getResult(latestVOList);
    }
}
